jQuery.fn.extend({
    /**
     * POST 一个表单
     * @param successFun 成功时，调用的方法
     * @param errorFun 错误时，调用的方法
     * @returns {*}
     */
    post: function (successFun, errorFun) {
        if (typeof successFun !== 'function' || !$.isFunction(successFun)) {
            successFun = function (data, form) {
                log(["POST response:", data]);
            };
        }
        if (typeof (errorFun) !== 'function' || !$.isFunction(errorFun)) {
            errorFun = function (data, form) {
                console.error(data);
            }
        }

        return this.each(function () {
            var data = $(this).serializeObject();
            var url = $(this).prop("action");
            var form = $(this);
            $.ajax({
                type: "POST",
                url: url,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(data)
            }).done(function (data) {
                if (data.code !== 200) {
                    errorFun(data, form);
                } else {
                    successFun(data, form);
                }
            }).fail(function () {
                errorFun({
                    code: 500,
                    message: '请求接口失败!'
                }, form);
            });

            return false;
        });
    },

    serializeObject: function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    },

    isMobile: function (trueFun, falseFun) {
        this.each(function () {
            var mobileInput = this;
            var val = $(mobileInput).val();
            if (/^1\d{10}$/.test(val)) {
                if ($.isFunction(trueFun)) {
                    trueFun(mobileInput, val);
                }
            } else {
                if ($.isFunction(falseFun)) {
                    falseFun(mobileInput, val);
                }
            }
            return mobileInput;
        });
    },

    setInputSuccess: function () {
        this.each(function () {
            $(this).addClass('form-control-success').removeClass('form-control-danger')
                .parent().removeClass('has-danger').addClass('has-success');
            return this;
        });
    },

    setInputError: function () {
        this.each(function () {
            $(this).addClass('form-control-danger').removeClass('form-control-success')
                .parent().addClass('has-danger').removeClass('has-success');
            return this;
        });
    }
});

var MH = {
    ajax: function (method, url, data, success, error) {
        $.ajax({
            type: method,
            url: url,
            contentType: 'application/json; charset=utf-8',
            data: data == null ? '' : JSON.stringify(data),
            error: error
        }).done(function (data) {
            if (data.code == 200) {
                success(data);
            } else {
                error(data);
            }
        }).fail(function (e) {
            console.error(e);
            error({
                code: 500,
                msg: '请求接口失败!'
            });
        });
    },
    get: function (url, success, error) {
        this.ajax('GET', url, null, success, error);
    },
    post: function (url, data, success, error) {
        this.ajax('POST', url, data, success, error);
    },
    del: function (url, success, error) {
        this.ajax('DELETE', url, null, success, error);
    },
    curl: function (method, url, data, success, error) {
        $.ajax({
            type: method,
            url: url,
            contentType: 'text/html;charset=UTF-8',
            data: data == null ? '' : JSON.stringify(data),
            error: error
        }).done(function (html) {
            success(html);
        }).fail(function (e) {
            console.error(e);
            error();
        });
    }
};

